package com.mashuai.utils.toolkit.core.msHandlebean;

import com.mashuai.utils.base.MsIocUtils;
import com.mashuai.utils.toolkit.achieve.HandleDataUtilsCore;
import com.mashuai.utils.toolkit.core.msHandlebean.dto.MsDictDto;
import org.apache.commons.collections.MapUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author mashuai
 * @ClassName: MsHandleDictUtils
 * @Description: 单独处理字典
 */
public class MsHandleDictUtils {
    private static HandleDataUtilsCore handleDataUtilsCore = MsIocUtils.getBean(HandleDataUtilsCore.class);

    /**
     * 获取字典值
     *
     * @param dictTypeList
     * @return
     * @author mashuai
     */

    public static Map<String, List<MsDictDto>> getDictData(List<String> dictTypeList) {
        return handleDataUtilsCore.getDictData(dictTypeList);
    }

    /**
     * 处理单个字典值
     * 用法：beanDict(data, tBean::getState, TestDictBean::setStataName, "state");
     * 解释：上面调用就是说对TestDictBean实体类的state字段进行字典处理，处理结果是虚拟字段stateNmae
     *
     * @param list         你传入的集合，需要是list<Bean>类型
     * @param field        需要处理的Bean里面的字段
     * @param setFieldName 处理完成后需要把字段赋值给哪个虚拟字段
     * @param dictType     字典类型
     * @return
     * @author mashuai
     */
    public static <M, S> void beanDict(List<M> list, Function<M, S> field, BiConsumer<M, String> setFieldName, String dictType) {
        Map<String, List<MsDictDto>> dictMap = getDictData(Arrays.asList(dictType));
        if (dictMap.size() > 0) {
            for (M m : list) {
                S fieldValue = field.apply(m);
                for (MsDictDto s : dictMap.get(dictType)) {
                    if (s.getValue().equals(String.valueOf(fieldValue))) {
                        setFieldName.accept(m, s.getLabel());
                        break;
                    }
                }
            }
        }
    }

    /**
     * 处理单个List<Map>类型字典的值
     *
     * @param list         list<Map>类型
     * @param field        需要处理的字典
     * @param setFieldName 处理后字段的别名
     * @param dictType     字典类型
     * @return
     * @author mashuai
     */
    public static void mapDict(List<Map<String, Object>> list, String field, String setFieldName, String dictType) {
        //对数据字典进行处理
        mapDictM(list, field, setFieldName, dictType);
    }

    public static void mapDict(List<Map<String, Object>> list, String field, String dictType) {
        //对数据字典进行处理
        mapDictM(list, field, field, dictType);
    }

    private static void mapDictM(List<Map<String, Object>> list, String field, String setFieldName, String dictType) {
        //对数据字典进行处理
        Map<String, List<MsDictDto>> dictMap = getDictData(Arrays.asList(dictType));
        if (dictMap.size() > 0) {
            list.stream().map(t -> {
                Map<String, Object> dataMap = t;
                for (MsDictDto HandleSysDictDto : dictMap.get(dictType)) {
                    if (HandleSysDictDto.getValue().equals(MapUtils.getString(dataMap, field))) {
                        dataMap.put(setFieldName, HandleSysDictDto.getLabel());
                        break;
                    }
                }
                return dataMap;
            }).collect(Collectors.toList());
        }
    }

}
